﻿using System;
using System.Windows.Forms;

public class MathQuiz : Form
{
    private Random random = new Random();
    private Label questionLabel;
    private TextBox answerTextBox;
    private Button submitButton;
    private Timer timer;
    private int score = 0;
    private int timeLimit = 10; // seconds
    private int correctAnswer;
    private int questionCount = 0;
    private const int maxQuestions = 20;

    public MathQuiz()
    {
        // 初始化组件
        questionLabel = new Label { Location = new System.Drawing.Point(20, 20), Width = 300 };
        answerTextBox = new TextBox { Location = new System.Drawing.Point(20, 50) };
        submitButton = new Button { Text = "Submit", Location = new System.Drawing.Point(20, 80) };
        submitButton.Click += SubmitButton_Click;

        Controls.Add(questionLabel);
        Controls.Add(answerTextBox);
        Controls.Add(submitButton);

        StartNewQuestion();
        SetupTimer();
    }

    private void SetupTimer()
    {
        timer = new Timer { Interval = 1000 }; // 1秒
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        timeLimit--;
        if (timeLimit <= 0)
        {
            MessageBox.Show("时间到！跳过此题。");
            questionCount++;
            StartNewQuestion();
        }
    }

    private void StartNewQuestion()
    {
        if (questionCount >= maxQuestions)
        {
            EndQuiz();
            return;
        }

        int num1 = random.Next(1, 100);
        int num2 = random.Next(1, 100);
        // 随机选择加法或减法
        bool isAddition = random.Next(0, 2) == 0;

        if (isAddition)
        {
            questionLabel.Text = $"What is {num1} + {num2}?";
            correctAnswer = num1 + num2;
        }
        else
        {
            questionLabel.Text = $"What is {num1} - {num2}?";
            correctAnswer = num1 - num2;
        }

        answerTextBox.Clear();
        timeLimit = 10; // 重置计时器
    }

    private void SubmitButton_Click(object sender, EventArgs e)
    {
        int userAnswer;
        questionCount++;

        if (int.TryParse(answerTextBox.Text, out userAnswer))
        {
            if (userAnswer == correctAnswer)
            {
                score++;
                MessageBox.Show("正确！");
            }
            else
            {
                MessageBox.Show($"错误！正确答案是 {correctAnswer}。");
            }
        }
        else
        {
            MessageBox.Show("请输入一个有效的数字。");
        }

        StartNewQuestion();
    }

    private void EndQuiz()
    {
        timer.Stop();
        int finalScore = (score * 100) / maxQuestions; // 计算得分
        MessageBox.Show($"测试结束！你的得分是: {finalScore} 分");
        Application.Exit(); // 退出应用程序
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MathQuiz());
    }
}
